package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int max = 0,min = 0,sum = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        }
            else {
                min = rightBorder;
                max = leftBorder;
            }
        for (int i = min; i <= max; i++) {
            if (i % 2 == 0) {
                sum+=i;
            }
        }
        return sum;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int max = 0,min = 0,sum = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        }
        else {
            min = rightBorder;
            max = leftBorder;
        }
        for (int i = min; i <= max; i++) {
            if (i % 2 != 0) {
                sum+=i;
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int sum = 0;
        Integer[] gather = (Integer[]) arrayList.toArray();
        for(int i=0;i<gather.length;i++){
            int m = gather[i] * 3 + 2;
            sum+=m;
        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        Integer[] gather = (Integer[]) arrayList.toArray();
        for(int i = 0;i < gather.length;i++){
            if(gather[i]%2!=0){
                int m = gather[i] * 3 + 2;
                gather[i]=m;
            }
        }
        List<Integer> integer = Arrays.asList(gather);
        return integer;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        Integer[] gather = (Integer[]) arrayList.toArray();
        int sum=0;
        for(int i=0;i<gather.length;i++){
            if(gather[i]%2!=0){
                int m = gather[i] * 3 + 5;
                sum+=m;
            }
        }
        return sum;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        double sum = 0;
        int a = 1;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        if (list.size() % 2 == 0) {
            sum = (list.get(list.size() / 2 - 1) + list.get(list.size() / 2)) / 2;
        } else {
            sum = list.get(list.size() / 2 + 1);
        }

        return sum;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        Integer[] gather = (Integer[]) arrayList.toArray();
        int sum=0,odd=0,average=0;
        for(int i=0;i<gather.length;i++){
            if(gather[i]%2==0){
                sum+=gather[i];
                odd++;
                average = sum/odd;
            }
        }
        return average;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        if(specialElment % 2 == 1) return false;
        for (Integer integer : arrayList) {
            if(integer % 2 == 0 && integer == specialElment){
                return true;
            }
        }
        return false;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        Set<Integer> set = new HashSet<>();
        for (Integer integer : arrayList) {
            if(integer % 2 == 0)set.add(integer);
        }
        return new ArrayList<>(set);
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        LinkedList<Integer> list1 = new LinkedList<>();
        LinkedList<Integer> list2 = new LinkedList<>();
        for (Integer integer : arrayList) {
            if(integer % 2 == 0){
                list1.add(integer);
            }else{
                list2.addFirst(integer);
            }
        }
        list1.addAll(list2);
        return list1;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {

        Integer[] gather = (Integer[]) arrayList.toArray();
        for(int i=0;i<gather.length;i++){
            int m = (gather[i]+gather[i+1])*3;
            gather[i]=m;
        }
        List<Integer> gathers = Arrays.asList(gather);
        return gathers;
    }
}
